package com.feihao.zhou.demo06PropertiesTest;

import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class PropertiesAndIO {
    public static void main(String[] args) throws IOException {
        //先将键值对写入文件：
        Properties pro = new Properties();
        pro.setProperty("张三","北京");
        pro.setProperty("李四","湖南");
        pro.setProperty("李四","333");
        pro.setProperty("小桑","广东");
        pro.setProperty("className","com.feihao.zhou.tengqixiang");

        FileWriter fw = new FileWriter("666.properties");
        pro.store(fw,null);

        fw.close();



        //读取配置文件：
        //
        FileReader fr = new FileReader("666.properties");
        //创建读取的properties集合：
        Properties properties = new Properties();
        //将文件中的键值对读取到properties集合中
        properties.load(fr);

        String value = properties.getProperty("小桑");
        System.out.println(value);

       /* FileInputStream fis = new FileInputStream("666.properties");   //字节流读取中文会乱码。（中文编码3个字节为一个字符）
        Properties properties = new Properties();
        properties.load(fis);

        String v1 = properties.getProperty("className");
        System.out.println("v："+ v1);*/

        //遍历集合：
        Set<Map.Entry<Object, Object>> entries = properties.entrySet();
        for (Map.Entry<Object, Object> entry : entries) {
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }
    }
}
